﻿//2013-12-09
using System;
using System.Collections;
using System.Collections.Generic;
using PSS.DAL.SetManage.BasicData;
using PSS.Entity.SetManage.BasicData;
using PSS.Utility;

namespace PSS.BLL.SetManage.BasicData
{
    public class VendorBLL
    {
        private readonly VendorDAL _dal = new VendorDAL();

        public void AddVendor(string data)
        {
            var model = new VendorEntity();
            var rows = (ArrayList)JsonHelper.Decode(data);
            var list = new List<VendorContactsEntity>();
            foreach (Hashtable row in rows)
            {
                model.VendorId = Guid.NewGuid();
                model.VendorCode = row["VendorCode"].ToString();
                model.VendorName = row["VendorName"].ToString();
                model.VCategoryId = new Guid(row["VCategoryId"].ToString());
                model.BalanceDate = Convert.ToDateTime(row["BalanceDate"]);
                model.Payable = Convert.ToDecimal(row["Payable"]);
                model.Advance = Convert.ToDecimal(row["Advance"]);
                model.VATRate = Convert.ToDecimal(row["VATRate"]);
                model.Remark = row["Remark"].ToString();
                model.ArrList = (ArrayList)row["arrList"];
                foreach (Hashtable item in model.ArrList)
                {
                    var cmodel = new VendorContactsEntity();
                    cmodel.VContactId = Guid.NewGuid();
                    cmodel.VendorId = model.VendorId;
                    cmodel.Linkman = item["Linkman"] != null ? item["Linkman"].ToString() : "";
                    cmodel.MobilePhone = item["MobilePhone"] != null ? item["MobilePhone"].ToString() : "";
                    cmodel.SpecialPlane = item["SpecialPlane"] != null ? item["SpecialPlane"].ToString() : "";
                    cmodel.QqMsn = item["QqMsn"] != null ? item["QqMsn"].ToString() : "";
                    cmodel.DeliveryAddress = item["DeliveryAddress"] != null ? item["DeliveryAddress"].ToString() : "";
                    if (item["Flag"] != null)
                    {
                        cmodel.Flag = item["Flag"].ToString() != "0";
                    }
                    else
                    {
                        cmodel.Flag = false;
                    }

                    list.Add(cmodel);
                }
            }
            if (_dal.AddVendor(model))
            {
                _dal.AddVendorContacts(list);
            }
        }

        public string GetVendorList(VendorQcEntity qcmodel)
        {
            var total = _dal.GetTotal(qcmodel);
            var list = _dal.GetVendorList(qcmodel);
            return JsonHelper.Encode(total, list);
        }

        public string GetVendorInfo(string id)
        {
            return JsonHelper.Encode(_dal.GetVendorInfo(id));
        }

        public string GetVendorContactsList(VendorContactsQcEntity qcmodel)
        {
            var list = _dal.GetVendorContactsList(qcmodel);
            return JsonHelper.Encode(list);
        }

        public void ModifyVendor(string data)
        {
            var model = new VendorEntity();
            var rows = (ArrayList)JsonHelper.Decode(data);
            var list = new List<VendorContactsEntity>();
            foreach (Hashtable row in rows)
            {
                model.VendorId = new Guid(row["VendorId"].ToString());
                model.VendorCode = row["VendorCode"].ToString();
                model.VendorName = row["VendorName"].ToString();
                model.VCategoryId = new Guid(row["VCategoryId"].ToString());
                model.BalanceDate = Convert.ToDateTime(row["BalanceDate"]);
                model.Payable = Convert.ToDecimal(row["Payable"]);
                model.Advance = Convert.ToDecimal(row["Advance"]);
                model.VATRate = Convert.ToDecimal(row["VATRate"]);
                model.Remark = row["Remark"].ToString();
                model.ArrList = (ArrayList)row["arrList"];
                foreach (Hashtable item in model.ArrList)
                {
                    var cmodel = new VendorContactsEntity();
                    cmodel.VContactId = Guid.NewGuid();
                    cmodel.VendorId = model.VendorId;
                    cmodel.Linkman = item["Linkman"] != null ? item["Linkman"].ToString() : "";
                    cmodel.MobilePhone = item["MobilePhone"] != null ? item["MobilePhone"].ToString() : "";
                    cmodel.SpecialPlane = item["SpecialPlane"] != null ? item["SpecialPlane"].ToString() : "";
                    cmodel.QqMsn = item["QqMsn"] != null ? item["QqMsn"].ToString() : "";
                    cmodel.DeliveryAddress = item["DeliveryAddress"] != null ? item["DeliveryAddress"].ToString() : "";
                    if (item["Flag"] != null)
                    {
                        cmodel.Flag = item["Flag"].ToString() != "0";
                    }
                    else
                    {
                        cmodel.Flag = false;
                    }

                    list.Add(cmodel);
                }
            }
            _dal.ModifyVendor(model);
            _dal.DeleteVendorContacts(model.VendorId.ToString());
            _dal.AddVendorContacts(list);
        }

        public bool RemoveVendor(string id)
        {
            var result = false;
            if (_dal.RemoveVendor(id))
            {
                _dal.DeleteVendorContacts(id);
                result = true;
            }
            return result;
        }

        public string GetVendorListByCombobox()
        {
            return JsonHelper.Encode(_dal.GetVendorListByCombobox());
        }
    }
}
